﻿using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using 修为尽失.辅助.exts;
using 修为尽失.辅助.Resources;

namespace 修为尽失.辅助
{
    public class IndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var  item = (ListViewItem)value;
            var lv = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
            if (item != null)
                if (lv != null)
                    return lv.ItemContainerGenerator.IndexFromContainer(item) + 1;
            return -1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public sealed class BytesLengthInfoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "<无>";
            }
            if(value is byte[] v)
            {
                return $"{v.Length}字节";
            }
            return "??";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public sealed class StringNullOrVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public sealed class MultiStringNullOrVisibilityConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool show = false;
            foreach (var v in values)
            {
                if (v != null && v.ToString() != "")
                {
                    show = true;
                    break;
                }
            }

            return show ? Visibility.Visible : Visibility.Collapsed;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public sealed class ListViewItemBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            ListViewItem item = (ListViewItem) value;
            ListView listView =
                ItemsControl.ItemsControlFromItemContainer(item) as ListView;
            if (item != null)
            {
                if (listView != null)
                {
                    int index =
                        listView.ItemContainerGenerator.IndexFromContainer(item);

                    if (index % 2 == 0)
                    {
                        return Brushes.White;
                    }
                }
            }

            return new SolidColorBrush(Color.FromRgb(240, 240, 240));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    

    public class ByteArrayImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return new BitmapImage(new Uri(ResManager.Paths.innerdir + "/Resources/Pictures/facewarning.png"));
            }

            if (value is byte[] bytes)
            {
                var bi = new BitmapImage();
                bi.BeginInit();
                using (var ms = new MemoryStream(bytes))
                {
                    bi.StreamSource = ms;
                }

                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.EndInit();
                return bi;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

   
     public class TimeSpanRotateSpeedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return TimeSpan.FromSeconds(1 / (double) value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 1d / ((TimeSpan) value).Seconds;
        }
    }

    public class Base64StringBitmapSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString().Base64StringToBitmapSource();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as BitmapSource).ToBase64String();
        }
    }

    public class BytesBitmapSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        { 
            return (value as byte[])?.ToBitmapSource();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(object), typeof(BitmapImage))]
    public class IconImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Image))
                return null;
            try
            {
                return new BitmapImage(new Uri(ResManager.Paths.innerdir + ((Image) value)?.Source, UriKind.RelativeOrAbsolute));
            }
            catch
            {
                try
                {
                    return new BitmapImage(new Uri(((Image) value)?.Source.ToString(), UriKind.Absolute));
                }
                catch
                {
                    return ((Image) value).Source;
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new Image()
            {
                Source = value as BitmapImage
            };
        }
    }

    public class ColorBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush b = null;
            if (value is Color)
            {
                b = new SolidColorBrush((Color) value);
            }

            return b;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as SolidColorBrush)?.Color;
        }
    }

    public class BrushColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as SolidColorBrush)?.Color;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush b = null;
            if (value is Color)
            {
                b = new SolidColorBrush((Color) value);
            }

            return b;
        }
    }

    public class HashLoadStateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new SolidColorBrush((Color) ColorConverter.ConvertFromString(parameter.ToString()));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ArrayStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ps = parameter?.ToString()?.Split(new[] {';'});
            var wl = 5;
            var addtext = "";
            if (ps != null && ps.Length > 1)
            {
                try
                {
                    addtext = ps[0].Replace("\\n", Environment.NewLine);
                    Int32.TryParse(ps[1], out wl);
                }
                catch (Exception ex)
                {
                    ex.ShowException();
                }
            }

            return addtext + (value as ulong[])?.ToPrintableString(wl, Environment.NewLine);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class PathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Path.GetFileName(value?.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

     

    public class DateTimeFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((DateTime) value).ToString(parameter?.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = value == null;
            if (parameter as string == "inverse")
            {
                return flag ? Visibility.Collapsed : Visibility.Visible;
            }

            return (!flag) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NullOrEmptyStringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = value == null;
            if (value is string)
            {
                flag = string.IsNullOrEmpty((string) value);
            }

            if (parameter as string == "inverse")
            {
                return flag ? Visibility.Collapsed : Visibility.Visible;
            }

            return (!flag) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = false;
            if (value is bool)
            {
                flag = (bool) value;
            }
            else if (value is bool?)
            {
                bool? nullable = (bool?) value;
                flag = (nullable.HasValue && nullable.Value);
            }

            if (parameter as string == "inverse")
            {
                return flag ? Visibility.Collapsed : Visibility.Visible;
            }

            return (!flag) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanToFontWeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = parameter as string == "inverse";
            bool? nullable = value as bool?;
            if (nullable.HasValue && nullable.Value)
            {
                return flag ? FontWeights.Normal : FontWeights.Bold;
            }

            return flag ? FontWeights.Bold : FontWeights.Normal;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}